package edu.java.homework.hw03;

import java.util.Scanner;

/**
 * Multiplication of matrixes
 * 
 * @author Tsvetan Vasilev
 * 
 */
public class MatrixMultiply {

    public static void main(String[] args) {

	Scanner input = new Scanner(System.in);
	int row1 = 0;
	int col1 = 0;
	int row2 = 0;
	int col2 = 0;
	System.out
		.println("Please, enter the dimentions of the first matrix. ");
	System.out.print("matrix1.rows= ");
	row1 = input.nextInt();
	System.out.print("matrix1.cols= ");
	col1 = input.nextInt();

	System.out
		.println("Please, enter the dimentions of the second matrix. ");
	System.out.print("matrix2.rows= ");
	row2 = input.nextInt();
	System.out.print("matrix2.cols= ");
	col2 = input.nextInt();

	if (row1 < 1 || row2 < 1 || col1 < 1 || col2 < 1) {
	    throw new IllegalArgumentException(
		    "You've entered incorrect dimention of the matrixes");
	}

	if (col1 != row2) {
	    throw new IllegalArgumentException("In multiplication of matrixes"
		    + " the count of columns of the first matrix would be the"
		    + " same as the count of the rows of the second matrix");
	}

	double[][] matrix1 = new double[row1][col1];
	double[][] matrix2 = new double[row2][col2];
	double[][] matrix3 = new double[row1][col2]; 

	System.out.println("Input the first matrix: ");
	inputMatrix(matrix1);
	printMatrix(matrix1);

	System.out.println("Input the second matrix: ");
	inputMatrix(matrix2);
	printMatrix(matrix2);
	
	multiplyMatrix(matrix1, matrix2, matrix3); 
	System.out.println("The multiplied matrix: ");
	printMatrix(matrix3);

	
	input.close();

	/*
	 * double[][] mat1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 0, 5, 9
	 * } }; double[][] mat2 = { { 1, 2 }, { 3, 4 }, { 0, 5 } }; double[][]
	 * mat3 = new double[4][2];
	 * 
	 * double[][] mat7 = {{5}}; double[][] mat8 = {{2}}; double[][] mat9 =
	 * new double[1][1];
	 * 
	 * 
	 * multiplyMatrix(mat1, mat2, mat3); printMatrix(mat3);
	 * 
	 * multiplyMatrix(mat7, mat8, mat9); printMatrix(mat9);
	 */
    }

    public static void multiplyMatrix(double[][] matrix1, double[][] matrix2,
	    double[][] matrix3) {
	if (matrix1 == null || matrix2 == null || matrix3 == null) {
	    throw new IllegalArgumentException("Null-pointed array");
	}

	if (matrix1.length < 1 || matrix2.length < 1 || matrix3.length < 1) {
	    throw new IllegalArgumentException(
		    "The matrix would contain one or more number");

	}

	int rows = matrix1.length;
	int cols = matrix2[0].length;
	int rowColIndex = matrix1[0].length;

	for (int i = 0; i < rows; i++) {
	    for (int l = 0; l < cols; l++) {
		double numb = 0;
		for (int j = 0; j < rowColIndex; j++) {
		    numb += matrix1[i][j] * matrix2[j][l];
		}
		matrix3[i][l] = numb;
	    }
	}

    }

    public static void inputMatrix(double[][] matrix) {
	if (matrix == null) {
	    throw new IllegalArgumentException("Null-pointed array");
	}

	Scanner input = new Scanner(System.in);
	for (int i = 0; i < matrix.length; i++) {
	    for (int j = 0; j < matrix[i].length; j++) {
		System.out.print("numb[" + i + "][" + j + "]= ");
		matrix[i][j] = input.nextDouble();
	    }
	}
    }

    public static void printMatrix(double[][] matrix) {
	System.out.println();
	for (int i = 0; i < matrix.length; i++) {
	    for (int j = 0; j < matrix[i].length; j++) {
		System.out.print(matrix[i][j] + "  ");
	    }
	    System.out.println();
	}
	System.out.println();
    }
}
